
*-------------------------*
*-------------------------*
*    Appendix Figures
*------------------------*
*-------------------------*


*************
* Figure A1
*************
		use created_dta/analysis_samp, clear 
egen qtr = group(year_arrival quarter_arrival)
	bys dmsl_tc_first qtr : egen meandep = mean(dep_win_3yr)
	bys dmsl_tc_first qtr: egen ct = total(1)
	g insamp_inst_3_yr_allterms = ((meandep*ct) - dep_win_3yr)/(ct-1)
	drop meandep ct 
drop _merge
merge 1:m pid using raw_dta\cas_unit_deploy_nopii_v3.dta, keep(1 3) nogen
gen incident_myr = mofd(incidentdt_s)
gen wia = 1 if dodgrp=="HIW"
gen serwia = 1 if dnu2=="S" & wia== 1
gen vserwia = 1 if dnu2=="V" & wia== 1
gen kia = 1 if dodgrp=="HDK" | dodgrp=="HDR" | dodgrp=="HDC" | dodgrp=="HDM"

g ym = ym(year_arrival,month_arrival)
cap drop BCT
egen BCT = group(dmsl_tc_first)
bys BCT ym: egen ct_in_data = total(1)
bys BCT incident_myr: egen BCT_wia = sum(wia)
bys BCT incident_myr: egen BCT_kia = sum(kia)

summ ct_in_data
replace ct_in_data = ct_in_data*10

summ BCT_wia
replace BCT_wia = BCT_wia*150
summ BCT_kia
replace BCT_kia = BCT_kia*500

keep dmsl_tc_first ym BCT post_orgraa shr_hfp_loo_0 ct_in_data BCT_wia BCT_kia incident_myr
duplicates drop 
duplicates drop BCT incident_myr, force

tsset BCT incident_myr
drop if incident_myr == .
g one_arrival_in_data = 1 
tsfill, full
drop if incident_myr > 671
drop if incident_myr < 541

replace one_arrival_in_data = 0 if one_arrival_in_data == .
replace ct_in_data = 0 if ct_in_data == . 
 g not_in_data = 1-one_arrival_in_data
replace not_in_data = 0.75 if not_in_data == 1
replace not_in_data = . if not_in_data == 0
replace BCT_wia = 0 if BCT_wia == .
replace BCT_kia = 0 if BCT_kia == .
bys BCT (incident_myr): replace dmsl_tc_first = dmsl_tc_first[_n-1] if dmsl_tc_first== ""
forvalues x = 1(1)100 {
  bys BCT (incident_myr): replace dmsl_tc_first = dmsl_tc_first[_n+1] if dmsl_tc_first== ""
}
g dmsl_tc = dmsl_tc_first
g file_myr = incident_myr

keep if dmsl_tc != ""

merge 1:1 dmsl_tc file_myr using "raw_dta/share_dep_by_dmsl_tc_month_whole_samp_v2", keep(1 3) 
g matched = _merge == 3
drop _merge

tab matched one_arrival_in_data

g deployed = any_hfp >= 0.5
replace deployed = . if any_hfp == .

format incident_myr %tm

g nodata = count_enlisted == .
replace nodata = 0.8 if nodata == 1
replace nodata = . if nodata == 0

gen dmsl_leng = length(dmsl_tc_first)
g dmsl_leng_temp = dmsl_leng - 3
g dmsl_temp = substr(dmsl_tc_first,1,dmsl_leng_temp)

foreach post in "Fort Bliss" "Schofield Barracks" {
    levelsof BCT if post_orgraa == "`post'", local(b)
	foreach BCT in `b' {
	    levelsof dmsl_temp if BCT == `BCT', local(lab)
		if "`BCT'" == "2" local outlab a 
		if "`BCT'" == "10" local outlab b 
		if "`BCT'" == "20" local outlab c 
		if "`BCT'" == "29" local outlab d 
		if "`BCT'" == "14" local outlab e 
		if "`BCT'" == "23" local outlab f 
		local lab2 = substr(`lab',1,length(`lab'))
		twoway (line any_hfp_an_samp incident_myr if BCT == `BCT', lpattern(-) lcolor(red)) ///
		(line count_enlisted incident_myr if BCT == `BCT', lcolor(gs7) yaxis(2)), ///
		subtitle("BCT = `lab2'" " Post = `post'") xtick(540(12)671)  xlabel(540 "2005" 552 "2006" 564 "2007" 576 "2008" 588 "2009" 600 "2010" 612 "2011" 624 "2012" 636 "2013" 648 "2014" 660 "2015" 672 "2016") ytitle("# of Enlisted Soldiers", axis(2)) ytitle("Share of Analysis Sample Deployed", axis(1)) xtitle("Month of Arrival")  ytick(0(.2)1, axis(1)) ylabel(0(.2)1, axis(1)) ytick(0(1000)5000, axis(2))  ylabel(0(1000)5000, axis(2)) legend(label(1 "Share Deployed") label(2 "# of ALL enlisted"))
	  graph export "${outputfolder}/FigureA1`outlab'.pdf", as(pdf) replace
	}
}

	
*************
* Figure A2
*************
use "created_dta/${analysissamp}", clear

local INST ${NEWINST}

preserve 
clear 
set obs 36
g time = .  
g beta = . 
g se = . 
g fitted10 = . 
g fitted90 = .
g fitted25 = . 
g fitted75 = .
save "created_dta/acr_weight_input", replace 
restore 

forvalues x = 1(1)34 {
	g mdepg`x' = mths_dep_win_3yr>=`x' 
}

forvalues t = 1(1)34 {
reghdfe mdepg`t' `INST' , absorb(${FE1}) vce(robust)
	local beta = _b[`INST']
	local se = _se[`INST'] 

foreach x in 10 25 75 90 {
preserve 
ppmlhdfe mdepg`t' `INST', absorb(${FE1}) vce(robust) d
summ `INST' if e(sample), det 
local pcut = r(p`x')
margins, dydx(`INST')
replace `INST' = `pcut'
predict p`x' 
summ p`x'
local p`x' = r(mean)
restore 
}

preserve 
use "created_dta/acr_weight_input", clear 
replace time = `t' if _n == `t'
replace beta = `beta' if _n == `t'
replace se = `se'  if _n == `t'
replace fitted10 = `p10'  if _n == `t'
replace fitted25 = `p25'  if _n == `t'
replace fitted75 = `p75'  if _n == `t'
replace fitted90 = `p90'  if _n == `t'
save "created_dta/acr_weight_input", replace
restore 
}

use "created_dta/acr_weight_input", clear
egen sumbeta = sum(beta)
g normb = beta/sumbeta 
g seupper = beta/sumbeta + se*1.96/sumbeta
g selower = beta/sumbeta - se*1.96/sumbeta

twoway (scatter normb time, mcolor(black) msize(small)) (rcap seupper selower time, lcolor(gs5)), xtitle("Months Dep >= d") xlabel(1(2)35) xtick(1(1)36) ytitle("ACR Weights") legend(off)
graph export  "${outputfolder}/FigureA2.pdf", as(pdf) replace 

*then run army_bounds.py in gurobi to get the extensive margin bounds.  



use "created_dta/${analysissamp}", clear

*************
* Figure A3
*************
	
	global Xs init_approx_age init_white init_black init_hispanic afqs_mi  any_moral_waiver init_afqsc init_afqsc_2 init_afqsc_3 married init_nrdep hsd_ged high_school  any_meps_med_fail_cd any_meps_alcohol_fail  any_meps_marijuana_fail any_meps_cocaine_fail any_meps_alcohol_tst_miss any_meps_marijuana_tst_miss any_meps_cocaine_tst_miss ace_first_asvabcl ace_first_asvabco ace_first_asvabel ace_first_asvabfa ace_first_asvabgm ace_first_asvabgt ace_first_asvabmm ace_first_asvabof ace_first_asvabsc cl_mi co_mi el_mi fa_mi gm_mi gt_mi mm_mi of_mi sc_mi
	
	cap drop pred_bal resid_endog resid_endog_adj resid_inst resid_inst_adj

	reghdfe dep_win_3yr $Xs, absorb(${FE1}) vce(robust)
	predict pred_bal
	
	reghdfe dep_win_3yr, a(${FE1}) vce(robust) resid
	predict resid_endog, resid
	summ dep_win_3yr if e(sample)
	g resid_endog_adj = resid_endog + r(mean)
	
	reghdfe ${NEWINST}, a(${FE1}) vce(robust) resid
	predict resid_inst, resid
	summ ${NEWINST} if e(sample)
	g resid_inst_adj = resid_inst + r(mean)

	reghdfe dep_win_3yr ${NEWINST}, a(${FE1}) vce(robust) resid
	reg resid_endog_adj resid_inst_adj, robust
	reghdfe pred_bal ${NEWINST}, a(${FE1}) vce(robust) resid
	reg pred_bal resid_inst_adj, robust

	
local bwidth 
local degree degree(1)

 summ resid_inst_adj, det
 local r1 = r(p1) 
 local r2 = r(p99) 
 _pctile resid_inst_adj, p(2.5, 97.5)
 local r1 = r(r1) 
 local r2 = r(r2) 

twoway (hist resid_inst_adj if inrange(resid_inst_adj,`r1',`r2'), bin(16) color(black) fcolor(white) yaxis(2)) (lpolyci pred_bal resid_inst_adj if inrange(resid_inst_adj,`r1',`r2'), `degree' `bwidth'  yaxis(1) lwidth(thin)) (lpolyci resid_endog_adj resid_inst_adj if inrange(resid_inst_adj,`r1',`r2'), ciplot(rline) `degree'  `bwidth' yaxis(1)),  ytitle("Density", axis(2)) ytitle("Any Deployment", axis(1)) xtitle("Peer Months Deployed") legend(off) xscale(range(2.5 10.5)) yscale(range(0 1)) xlabel(3(2)11) ylabel(0(.2)1, axis(1)) text(0.6 9.5 "Predicted Any Deployment" 0.8 8.25 "Any Deployment", size(vsmall)) ///
text(0.95 3.5 "1st Stage Coef: 0.0576" 0.925 4.2 "(0.0005)" 0.9 4.1 "N: 157,415" 0.875 3.55 "Dep. Var. Mean:   0.6318", size(vsmall)) ///
text(0.75 3.7 "Pred Dep.: -0.00001" 0.725 4.2 "(0.00004)" 0.7 4.1 "N: 157.415" 0.675 3.55 "Dep. Var. Mean:   0.6318", size(vsmall)) ///
yscale(alt) yscale(alt axis(2)) 
	graph export "${outputfolder}/FigureA3.pdf", as(pdf) replace





*************
* Figure A4
*************

use "created_dta/${analysissamp}", clear
merge 1:1 pid using raw_dta/bar_or_misc_in.dta , nogen keep(1 3)
foreach var of varlist bar_or_misconduct_in* {
	replace `var' = 0 if `var' == . 
}

foreach var in   any_nvf_in_  bar_or_misconduct_in_    { 
preserve 
clear 
set obs 32
g beta = . 
g se = . 
g N = . 
g y0_beta =. 
g sey0 = . 
g Ny0 = .
g rawmean = . 
g mean = . 
g outcome = ""
g time = _n
save "created_dta/figA4_dta_`var'", replace 
restore 


global whichdep dep_win_3yr
global whichdep2 dep_win_3yr
global NEWENDOG mths_${whichdep}


forvalues t = 1(1)32 { 

sum `var'`t'
if r(mean)!= 0 {
cap drop Y0_`t'
g Y0_`t' = -`var'`t'*(1-${whichdep})

 ivreghdfe Y0_`t' (${whichdep} = ${NEWINST}) if ${rest} >= `t', absorb(${FE1}) robust $ivregsettings
summ `var'`t' if dep_win_3yr == 0 & e(sample)
local rawmean = r(mean)
summ `var'`t' if e(sample)
local mean = r(mean)

	preserve 
	use "created_dta/figA4_dta_`var'", clear 
	replace y0_beta = _b[${whichdep}] if time == `t'
	replace sey0 = _se[${whichdep}] if time == `t'
	replace N = e(N) if  time == `t'
	replace rawmean = `rawmean' if  time == `t'
	replace mean = `mean' if  time == `t'	
	save "created_dta/figA4_dta_`var'", replace 
	restore
	
 ivreghdfe `var'`t' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `t', absorb(${FE1}) robust $ivregsettings
	preserve 
	use "created_dta/figA4_dta_`var'", clear 
	replace beta = _b[${NEWENDOG}] if time == `t'
	replace se = _se[${NEWENDOG}] if time == `t'
	save "created_dta/figA4_dta_`var'", replace 
	restore
}
}
}


foreach var in any_nvf_in_  bar_or_misconduct_in_  { 
use "created_dta/figA4_dta_`var'", clear
replace y0_beta = y0_beta*100 
replace  beta = beta*100
replace se = se*100

g y10 = (y0_beta + 10*beta)
g ub10 = y10 + 1.96*10*se 
g lb10 = y10 - 1.96*10*se 

if "`var'" == "any_nvf_in_" local ylab "Any Non-violent Felony"
if "`var'" == "bar_or_misconduct_in_" local ylab "Separated for Misconduct or Barred"

if "`var'" == "any_nvf_in_" local which a
if "`var'" == "bar_or_misconduct_in_" local which b

format y10 y0_beta %09.1fc
g cohortstart = time
twoway (scatter y10 cohortstart , mcolor(black) yaxis(1)) (line y10 cohortstart , lcolor(black) yaxis(1))  (rcap ub10 lb10 cohortstart , lcolor(gs7) yaxis(1))  (scatter y0_beta cohortstart , mcolor(gs5) yaxis(1)) (line y0_beta cohortstart , lcolor(gs5) yaxis(1)),  legend(order(1 4) label(1 "Deploying Compliers (10 Months Dep.)") label(4 "Nondeploying Compliers")) xtitle("Quarters after arrival") ytitle("`ylab'") ///
 xscale(range(0 32)) xtick(0(2)32) xlabel(0(6)32) 
graph export "${outputfolder}/FigureA4`which'.pdf", replace as(pdf) 
}




use "created_dta/${analysissamp}", clear


*************
* Figure A5
*************	
	global Xs init_approx_age init_white init_black init_hispanic afqs_mi  any_moral_waiver init_afqsc init_afqsc_2 init_afqsc_3 married init_nrdep hsd_ged high_school  any_meps_med_fail_cd any_meps_alcohol_fail  any_meps_marijuana_fail any_meps_cocaine_fail any_meps_alcohol_tst_miss any_meps_marijuana_tst_miss any_meps_cocaine_tst_miss ace_first_asvabcl ace_first_asvabco ace_first_asvabel ace_first_asvabfa ace_first_asvabgm ace_first_asvabgt ace_first_asvabmm ace_first_asvabof ace_first_asvabsc cl_mi co_mi el_mi fa_mi gm_mi gt_mi mm_mi of_mi sc_mi
	
preserve 
keep if  num_qtr_allowed >= 32 

	cap drop pred_bal resid_endog resid_endog_adj resid_inst resid_inst_adj
	reghdfe combat_death_by_32 $Xs, absorb(${FE1}) vce(robust)
	predict pred_bal
	
	reghdfe combat_death_by_32, a(${FE1}) vce(robust) resid
	predict resid_endog, resid
	summ combat_death_by_32 if e(sample)
	g resid_endog_adj = resid_endog + r(mean)
	
	reghdfe ${NEWINST}, a(${FE1}) vce(robust) resid
	predict resid_inst, resid
	summ ${NEWINST} if e(sample)
	g resid_inst_adj = resid_inst + r(mean)

	reghdfe combat_death_by_32 ${NEWINST}, a(${FE1}) vce(robust) resid
	reg resid_endog_adj resid_inst_adj, robust
	reghdfe pred_bal ${NEWINST}, a(${FE1}) vce(robust) resid
	reg pred_bal resid_inst_adj, robust

local bwidth bwidth(1.5)
local degree degree(1)


summ combat_death_by_32, det
summ resid_endog_adj, det 
 summ resid_inst_adj, det
 local r1 = r(p1) 
 local r2 = r(p99) 
  _pctile resid_inst_adj, p(2.5, 97.5)
 local r1 = r(r1) 
 local r2 = r(r2) 

 twoway (hist resid_inst_adj if inrange(resid_inst_adj,`r1',`r2'), bin(16) color(black) fcolor(white) yaxis(2)) (lpolyci pred_bal resid_inst_adj if inrange(resid_inst_adj,`r1',`r2'), `degree' `bwidth'  yaxis(1) lwidth(thin)) (lpolyci resid_endog_adj resid_inst_adj if inrange(resid_inst_adj,`r1',`r2'), ciplot(rline) `degree'  `bwidth' yaxis(1)),  ytitle("Density", axis(2)) ytitle("Combat Death", axis(1)) xtitle("Peer Months Deployed") legend(off) xscale(range(4.5 13.5)) yscale(range(-.3 .9)) xlabel(4(2)13) ylabel(-.3(.2).9, axis(1)) text(.539 6.25 "Predicted Combat Death" .72 11.9 "Combat Death", size(vsmall)) ///
yscale(alt) yscale(alt axis(2)) ///
text(.96 6.5 "Reduced Form Coef: 0.046" .92 7.2 "(0.010)" .88 7.1 "N:  101,387" .84 6.55 "Dep. Var. Mean:   0.50", size(vsmall)) ///
text(.765 6.62 "Pred Dep.: -0.0001" .72 7.2 "(0.0001)" .68 7.1 "N:  101,387" .64 6.55 "Dep. Var. Mean:   0.50", size(vsmall))

	graph export "${outputfolder}/FigureA5a.pdf", as(pdf) replace
restore 

preserve 
keep if num_qtr_allowed >= 32 
	cap drop pred_bal resid_endog resid_endog_adj resid_inst resid_inst_adj
	reghdfe noncom_death_by_32 $Xs, absorb(${FE1}) vce(robust)
	predict pred_bal
	
	reghdfe noncom_death_by_32, a(${FE1}) vce(robust) resid
	predict resid_endog, resid
	summ noncom_death_by_32 if e(sample)
	g resid_endog_adj = resid_endog + r(mean)
	
	reghdfe ${NEWINST}, a(${FE1}) vce(robust) resid
	predict resid_inst, resid
	summ ${NEWINST} if e(sample)
	g resid_inst_adj = resid_inst + r(mean)

	reghdfe noncom_death_by_32 ${NEWINST}, a(${FE1}) vce(robust) resid
	reg resid_endog_adj resid_inst_adj, robust
	reghdfe pred_bal ${NEWINST}, a(${FE1}) vce(robust) resid
	reg pred_bal resid_inst_adj, robust

local bwidth bwidth(1)
local degree degree(1)


 summ resid_inst_adj, det
 _pctile resid_inst_adj, p(2.5, 97.5)
 local r1 = r(r1) 
 local r2 = r(r2) 
twoway (hist resid_inst_adj if inrange(resid_inst_adj,`r1',`r2'), bin(16) color(black) fcolor(white) yaxis(2)) (lpolyci pred_bal resid_inst_adj if inrange(resid_inst_adj,`r1',`r2'), `degree' `bwidth'  yaxis(1) lwidth(thin)) (lpolyci resid_endog_adj resid_inst_adj if inrange(resid_inst_adj,`r1',`r2'), ciplot(rline) `degree'  `bwidth' yaxis(1)),  ytitle("Density", axis(2)) ytitle("Noncombat Death", axis(1)) xtitle("Peer Months Deployed") legend(off) xscale(range(4.5 13.5)) yscale(range(0 3)) xlabel(4(2)13) ylabel(0(.5)3, axis(1)) text(1.15 5.6 "Predicted Noncombat Death" 1.06 12 "Noncombat Death", size(vsmall)) ///
yscale(alt) yscale(alt axis(2)) ///
text(2.9 5.5 "Reduced Form Coef: 0.004" 2.8 6.2 "(0.017)" 2.7 6.1 "N:  101,387" 2.6 5.55 "Dep. Var. Mean:   1.25", size(vsmall)) ///
text(2.4 5.7 "Pred Dep.: 0.0003" 2.3 6.2 "(0.0007)" 2.2 6.1 "N:  101,387" 2.1 5.55 "Dep. Var. Mean:   1.25", size(vsmall))


	graph export "${outputfolder}/FigureA5b.pdf", as(pdf) replace
restore	

preserve 
keep if num_qtr_allowed >= 32 
	cap drop pred_bal resid_endog resid_endog_adj resid_inst resid_inst_adj
	reghdfe amtvadc_in_32 $Xs, absorb(${FE1}) vce(robust)
	predict pred_bal
	
	reghdfe amtvadc_in_32, a(${FE1}) vce(robust) resid
	predict resid_endog, resid
	summ amtvadc_in_32 if e(sample)
	g resid_endog_adj = resid_endog + r(mean)
	
	reghdfe ${NEWINST}, a(${FE1}) vce(robust) resid
	predict resid_inst, resid
	summ ${NEWINST} if e(sample)
	g resid_inst_adj = resid_inst + r(mean)

	reghdfe amtvadc_in_32 ${NEWINST}, a(${FE1}) vce(robust) resid
	reg resid_endog_adj resid_inst_adj, robust
	reghdfe pred_bal ${NEWINST}, a(${FE1}) vce(robust) resid
	reg pred_bal resid_inst_adj, robust

local bwidth bwidth(1)
local degree degree(1)


 summ resid_inst_adj, det

  _pctile resid_inst_adj, p(2.5, 97.5)
 local r1 = r(r1) 
 local r2 = r(r2) 

twoway (hist resid_inst_adj if inrange(resid_inst_adj,`r1',`r2'), bin(16) color(black) fcolor(white) yaxis(2)) (lpolyci pred_bal resid_inst_adj if inrange(resid_inst_adj,`r1',`r2'), `degree' `bwidth'  yaxis(1) lwidth(thin)) (lpolyci resid_endog_adj resid_inst_adj if inrange(resid_inst_adj,`r1',`r2'), ciplot(rline) `degree'  `bwidth' yaxis(1)),  ytitle("Density", axis(2)) ytitle("Amt VADC", axis(1)) xtitle("Peer Months Deployed") legend(off) xscale(range(4.5 13.5)) yscale(range(4000 8000)) xlabel(4(2)13) ylabel(4000(1000)8000, axis(1)) text(34.5 12.5 "Predicted VADC Amt" 42.5 12.5 "Amt VADC", size(vsmall)) ///
yscale(alt) yscale(alt axis(2)) ///
text(7800 5.5 "Reduced Form Coef: 247.90" 7650 6.2 "(15,89)" 7500 6.1 "N:  101,387" 7350 5.55 "Dep. Var. Mean:   6117", size(vsmall)) ///
text(7100 5.7 "Pred Dep.: 1.42" 6950 6.2 "(2.57)" 6800 6.1 "N:  101,387" 6650 5.55 "Dep. Var. Mean:   6117", size(vsmall))


	graph export "${outputfolder}/FigureA5c.pdf", as(pdf) replace
restore


*************
* Figure A6
*************


use "created_dta/${analysissamp}", clear
g mths_dep_32 = mths_dep_win_3yr
g cdeath_by_12 = combat_death_by_12
replace cdeath_by_12 = 0 if dep_win_3yr==0
g cas_rate_by_32 = (cdeath_by_12==100)|(wia_by_12==100)
replace cas_rate_by_32= cas_rate_by_32*100
set seed 1987
global runlist anyvadc_in_ amtvadc_in_ noncom_death_by_  cas_rate_by_ 
global asof 32 
keep if ${rest} >= 32

egen bctyr = group(dmsl_tc_first qtr)
summ bctyr
global bctcnt 810 

preserve
bys bctyr: egen cnt = total(1)
keep bctyr year_arrival qtr cnt 
duplicates drop
isid bctyr
tempfile binfo 
save `binfo', replace
restore

levelsof bctyr, local(a)
foreach bctyr in `a' {
g bctyr_`bctyr' = (bctyr==`bctyr')
g mdXbctyr_`bctyr' = mths_dep_win_3yr * (bctyr==`bctyr')
}

preserve
collapse mths_dep_win_3yr, by(bctyr)
summ mths_dep_win_3yr, det 
restore

preserve
clear 
set obs $bctcnt
g bctyr = _n 
foreach outcome in $runlist {
g b`outcome' = . 
g se`outcome' = . 
}
merge 1:1 bctyr using `binfo', keep(1 3) nogen
save "created_dta/coefs_iv_qtr", replace
restore

foreach outcome in $runlist {
	
*fix the baseyr 
cap drop bctyr615 //  Largest, 3rd Cavalry Regiment Fort Hood in 2006Q3.
cap drop mdXbctyr615

local end = $bctcnt - 1
set seed 1987
ivreghdfe `outcome'${asof} (mdXbctyr_* = bctyr_*) if ${rest} >= ${asof}, absorb($FE1) robust $ivregsettings
	preserve
	use "created_dta/coefs_iv_qtr", clear
	forvalues bct = 1(1)`end' {
	replace b`outcome' = _b[mdXbctyr_`bct']*${SCALE} if bctyr == `bct'
	replace se`outcome' = _se[mdXbctyr_`bct']*${SCALE} if bctyr == `bct'
	}
	save "created_dta/coefs_iv_qtr", replace 
	restore
}

use "created_dta/coefs_iv_qtr", clear 
foreach var of varlist * {
	replace `var' = . if `var' == 0 
}

cap log close 
log using "${outputfolder}/FigureA6regs.txt", text replace
qui {
foreach out in $runlist {
cap drop weight
g weight = 1/se`out'



preserve
	drop if cnt < 100 
		*output reg coefs for top right hand corner of figure
		noi disp "reg dropping < 100: `out'"
		noi reg b`out' bcas_rate_by_ [aweight = weight], robust

	summ b`out', d
	drop if b`out' <= r(p1) |  b`out' >= r(p99)


	if "`out'" == "anyvadc_in_" local lab "Any VADC Receipt"
	if "`out'" == "amtvadc_in_" local lab "VADC Amount"
	if "`out'" == "noncom_death_by_" local lab "Noncombat Death"

	if "`out'" == "anyvadc_in_" local which a
	if "`out'" == "amtvadc_in_" local which b
	if "`out'" == "noncom_death_by_" local which c

	twoway (lfit b`out' bcas_rate_by_ [aweight = weight])  (scatter b`out' bcas_rate_by_ [aweight = weight] , msymbol(o) mcolor( black   ) msize(tiny)), ytitle("`lab'") xtitle("Casualty (WIA or Combat Death)") legend(off) 
	
	if "`out'" != "cas_rate_by_" graph export "${outputfolder}/FigureA6`which'.pdf", as(pdf) replace
	
restore
}
}


*************
* Figure A7
*************


use "created_dta/${analysissamp}", clear

xtile instgrp = ${NEWINST} if num_qtr_allowed>=32, nq(4)
xtile inst4tile = ${NEWINST} if num_qtr_allowed>=32, nq(4)
xtile inst5tile = ${NEWINST} if num_qtr_allowed>=32, nq(5)
xtile cas4tile = casnotsd if num_qtr_allowed>=32, nq(4)
xtile cas5tile = casnotsd if num_qtr_allowed>=32, nq(5)

gen casgrp = 1 if casnotsd <=0
replace casgrp = 2 if casnotsd>0 & casnotsd<=4
replace casgrp = 3 if casnotsd>4 & casnotsd<=8
replace casgrp = 4 if casnotsd>8 & casnotsd<.

forvalues k=1(1)4 {
	gen instgrp`k' = instgrp == `k'
	gen casgrp`k' = casgrp == `k'
	gen instcasgrp`k' = ${NEWINST}*casgrp`k'
	gen depcasgrp`k' = ${NEWENDOG}*casgrp`k'
}

forvalues k=1(1)4 {
	forvalues j=1(1)4{
		gen inst`k'_cas`j' = (instgrp`k'==1 & casgrp`j'==1)
		gen inst`k'_cas`j'_4tile = (inst4tile==`k' & cas4tile==`j')
	}
}

forvalues k=1(1)5 {
	forvalues j=1(1)5{
		gen inst`k'_cas`j'_5tile = (inst5tile==`k' & cas5tile==`j')
	}
}

foreach dv in   wia_by_32  amtvadc_in_32   noncom_death_by_32  ndi_despair2_by_32  {
	
preserve
clear 
set obs 25
g n = _n 
gen inst_5tile = .
gen cas_5tile = .
gen beta = . 
gen se = .
gen lbci = .
gen ubci = .
gen mean_inst1_cas1 = .
local i = 1
foreach a in 1 2 3 4 5 { //3
	foreach b in 1 2 3 4 5 {
		replace inst_5tile = `a' if n == `i'
		replace cas_5tile = `b' if n == `i'
		local i = `i'+1
	}

}

tempfile store 
save `store', replace
restore


 reghdfe `dv' inst1_cas2_5tile inst1_cas3_5tile inst1_cas4_5tile inst1_cas5_5tile inst2_cas1_5tile inst2_cas2_5tile inst2_cas3_5tile inst2_cas4_5tile inst2_cas5_5tile inst3_cas1_5tile inst3_cas2_5tile inst3_cas3_5tile inst3_cas4_5tile inst3_cas5_5tile inst4_cas1_5tile inst4_cas2_5tile inst4_cas3_5tile inst4_cas4_5tile inst4_cas5_5tile inst5_cas1_5tile inst5_cas2_5tile inst5_cas3_5tile inst5_cas4_5tile inst5_cas5_5tile, absorb($FE1) vce(robust)
	sum `dv' if e(sample) & inst1_cas1_5tile==1
	local mean_base=r(mean)
 
preserve
use `store', replace
	replace mean_inst1_cas1=`mean_base'
	replace beta = . if inst_5tile == 1 & cas_5tile == 1
	replace se = . if inst_5tile == 1 & cas_5tile == 1
	foreach b in 2 2 3 4 5 {
		replace beta = _b[inst1_cas`b'_5tile]  if inst_5tile == 1 & cas_5tile == `b'
		replace se = _se[inst1_cas`b'_5tile]  if inst_5tile == 1 & cas_5tile == `b'
		replace lbci = _b[inst1_cas`b'_5tile] - 1.96 * _se[inst1_cas`b'_5tile] if inst_5tile == 1 & cas_5tile == `b'
		replace ubci = _b[inst1_cas`b'_5tile] + 1.96 * _se[inst1_cas`b'_5tile] if inst_5tile == 1 & cas_5tile == `b'
	}
foreach a in  2 3 4 5 { //3
	foreach b in 1 2 3 4 5 {
		replace beta = _b[inst`a'_cas`b'_5tile]  if inst_5tile == `a' & cas_5tile == `b'
		replace se = _se[inst`a'_cas`b'_5tile] if inst_5tile == `a' & cas_5tile == `b'
		replace lbci = _b[inst`a'_cas`b'_5tile] - 1.96 * _se[inst`a'_cas`b'_5tile]  if inst_5tile == `a' & cas_5tile == `b'
		replace ubci = _b[inst`a'_cas`b'_5tile] + 1.96 * _se[inst`a'_cas`b'_5tile]  if inst_5tile == `a' & cas_5tile == `b'
	}
}

save "created_dta/`dv'_rf_inst_cas_5tile", replace
restore
}


use "created_dta/wia_by_32_rf_inst_cas_5tile.dta", clear
rename cas_5tile cas_group
rename inst_5tile inst_qtile
replace beta = 0 if cas_group == 1 &  inst_qtile == 1
replace beta = beta + mean_inst1_cas1

heatplot beta cas_group  inst_qtile, values(format(%4.3f))  statistic(mean) color(grey) cut({@min-0.5}(0.1){@max+2}) legend(off) xtitle("Avg. deployment length of peers (quintiles)") ytitle("Rate of peer casualtie (quintiles)") xlab(1(1)5) ylab(1(1)5)  graphregion(color(white))

graph export "${outputfolder}/FigureA7a.pdf", replace


use "created_dta/amtvadc_in_32_rf_inst_cas_5tile.dta", clear
rename cas_5tile cas_group
rename inst_5tile inst_qtile
replace beta = 0 if cas_group == 1 &  inst_qtile == 1
replace beta = beta + mean_inst1_cas1


heatplot beta cas_group  inst_qtile, values(format(%4.0f))  statistic(mean) color(grey) cut({@min-500}(500){@max+1600}) legend(off) xtitle("Avg. deployment length of peers (quintiles)") ytitle("Rate of peer casualties (quintiles)") xlab(1(1)5) ylab(1(1)5) graphregion(color(white)) 
graph export "${outputfolder}/FigureA7b.pdf", replace

use "created_dta/noncom_death_by_32_rf_inst_cas_5tile.dta", clear

rename cas_5tile cas_group
rename inst_5tile inst_qtile

replace beta = 0 if cas_group == 1 &  inst_qtile == 1
replace beta = beta + mean_inst1_cas1

heatplot beta cas_group  inst_qtile, values(format(%4.3f))  statistic(mean) color(grey) cut({@min-0.5}(0.1){@max+1}) legend(off) xtitle("Avg. deployment length of peers (quintiles)") ytitle("Rate of peer casualties (quintiles)") xlab(1(1)5) ylab(1(1)5)  graphregion(color(white))
graph export "${outputfolder}/FigureA7c.pdf", replace


use "created_dta/ndi_despair2_by_32_rf_inst_cas_5tile.dta", clear
rename cas_5tile cas_group
rename inst_5tile inst_qtile
replace beta = 0 if cas_group == 1 &  inst_qtile == 1
replace beta = beta + mean_inst1_cas1

heatplot beta cas_group  inst_qtile, values(format(%4.3f))  statistic(mean) color(grey) cut({@min-0.5}(0.1){@max+1}) legend(off) xtitle("Avg. deployment length of peers (quintiles)") ytitle("Rate of peer casualties (quintiles)") xlab(1(1)5) ylab(1(1)5)  graphregion(color(white))
graph export "${outputfolder}/FigureA7d.pdf", replace






use "created_dta/${wholesamp}", clear
forv i = 1/36{
	gen amtssissdi_in_`i' = amtdisability_in_`i' - amtvadc_in_`i'
}


*************
* Figure A8 
*************
	preserve
		collapse mths_dep_win_3yr combat_death_by_12, by(year_arrival)
		keep if inrange(year_arrival,2001,2013)
	twoway (line mths_dep_win_3yr year_arrival, lcolor(gs3) lpattern(longdash) lwidth(medthick) yaxis(1)) ///
	 (line combat_death_by_12 year_arrival, lcolor(gs10) lpattern(-) lwidth(medthick) yaxis(2)), ///
	 ytitle("Months Deployed", axis(1)) ytitle("Combat Death (x100)", axis(2)) ylabel(, format(%9.2fc) axis(1)) ylabel(, format(%9.2fc) axis(2)) ///
	xtick(2001(1)2013) xlabel(2001(1)2013) xtitle("Cohort") legend(label(1 "Months Deployed") label(2 "Combat Death (x100)") order(1 2))
	
	graph export "${outputfolder}/FigureA8.pdf", as(pdf) replace
	
	restore




*************
* Figure A9
*************
	global Xs init_approx_age init_white init_black init_hispanic female afqs_mi  any_moral_waiver init_afqsc init_afqsc_2 init_afqsc_3 married init_nrdep hsd_ged high_school  any_meps_med_fail_cd any_meps_alcohol_fail  any_meps_marijuana_fail any_meps_cocaine_fail any_meps_alcohol_tst_miss any_meps_marijuana_tst_miss any_meps_cocaine_tst_miss ace_first_asvabcl ace_first_asvabco ace_first_asvabel ace_first_asvabfa ace_first_asvabgm ace_first_asvabgt ace_first_asvabmm ace_first_asvabof ace_first_asvabsc cl_mi co_mi el_mi fa_mi gm_mi gt_mi mm_mi of_mi sc_mi
	
	qui {
foreach wspc in IV {
foreach fe in FE1_as {
foreach dvx in wia_by_ noncom_death_by_ amtvadc_in_ amtssissdi_in_ { 
	foreach timehorizon in 8 {

if "`dvx'" == "wia_by_" local lab "Combat Injury" 
if "`dvx'" == "noncom_death_by_" local lab "Non-Combat Death"
if "`dvx'" == "amtvadc_in_" local lab "Amt VADC" 
if "`dvx'" == "amtssissdi_in_" local lab "Amt SSI/SSDI"

local x = 4*(`timehorizon')

local dv `dvx'

preserve
local yr1 2001
if inlist("`dv'","inc_combined_by_","rec_any_forc_by_") {
	drop if year_arrival == 2001
	local yr1 2002
}

local midyr 2006 

if inlist("`dv'","inc_combined_by_","noncom_death_by_","ndi_despair2_by_","ndi_suicide_plus2_by_") local midyr 2008
if inlist("`dv'","ndi_war_by_","combat_death_by_","wia_by_") local midyr 2005

local endyr = 2019 - `timehorizon'
if "`wspc'" == "IV" noi ivreghdfe `dv'`x' $Xs an_samp_dum (${NEWENDOG}  = ${NEWINST} ) if ${rest} >= `x' , absorb(FE = ${`fe'}) robust $ivregsettings
keep if e(sample)

predict pred_X_and_dep 
if "`wspc'" == "IV" {
g pred_dep = _b[${NEWENDOG}]*${NEWENDOG}
g pred_X_no_dep = pred_X_and_dep -  _b[${NEWENDOG}]*${NEWENDOG}
}
g pred_FE = FE


g C = 1
noi summ year_arrival
collapse (mean)   `dv'`x' pred_X_and_dep pred_X_no_dep pred_dep pred_FE  (sum) C, by(year_arrival)

noi tab year_arrival
noi list *

foreach v in `dv'`x' pred_X_and_dep pred_X_no_dep pred_dep pred_FE  {
summ `v' if year_arrival == `yr1' 
replace `v'= `v'-r(mean)
}

noi list * 

local ydec 3
if inlist("`dvx'","amtdisability_in_","amtvadc_in_","amtssissdi_in_") local ydec 0
if inlist("`dvx'","wia_by_","noncom_death_by_") local ydec 1
 
local deplab = "Dep"
if "`wspc'" == "twoinst_cas" local deplab "Dep. and Peer Cas."
if "`wspc'" == "twoinst_wia" local deplab "Dep and Peer WIA"
if "`wspc'" == "twoinst_kia" local deplab "Dep and Peer KIA"

twoway ///
 (line pred_X_and_dep year_arrival, lcolor("155 0 0") lpattern(--.) lwidth(medthick) yaxis(1)) ///
(line pred_X_no_dep year_arrival, lcolor("185 140 80") lpattern(longdash) lwidth(medthick) yaxis(1)) ///
 (line `dv'`x' year_arrival, lcolor("gs4") lpattern(line) lwidth(medthick) yaxis(1)) ///
 (line pred_dep year_arrival, lcolor("85 140 180") lpattern(-) lwidth(medthick) yaxis(1)) ///\
, yline(0, lcolor(gs3) lwidth(thin)) ytitle("`lab' (Relative to 2001)", axis(1)) ylabel(, format(%9.`ydec'fc)) ///
xtick(`yr1'(1)`endyr') xlabel(`yr1'(1)`endyr') xtitle("Cohort") legend(label(1 "Explained by Xs and `deplab'") label(2 "Explained by Xs") label(3 "Observed Outcome") label(4 "Explained by `deplab'") order(3 2 4 1) symxsize(small) size(small)) name(figA9_`dvx', replace)

if "`dvx'" == "wia_by_" local outlab a 
if "`dvx'" == "noncom_death_by_" local outlab b
if "`dvx'" ==  "amtvadc_in_" local outlab c
if "`dvx'" == "amtssissdi_in_" local outlab d 

graph export "${outputfolder}/FigureA9`outlab'.pdf", as(pdf) replace

restore
}
}
}
}
}



*************
* Figure A10 
*************
	global Xs init_approx_age init_white init_black init_hispanic female afqs_mi  any_moral_waiver init_afqsc init_afqsc_2 init_afqsc_3 married init_nrdep hsd_ged high_school  any_meps_med_fail_cd any_meps_alcohol_fail  any_meps_marijuana_fail any_meps_cocaine_fail any_meps_alcohol_tst_miss any_meps_marijuana_tst_miss any_meps_cocaine_tst_miss ace_first_asvabcl ace_first_asvabco ace_first_asvabel ace_first_asvabfa ace_first_asvabgm ace_first_asvabgt ace_first_asvabmm ace_first_asvabof ace_first_asvabsc cl_mi co_mi el_mi fa_mi gm_mi gt_mi mm_mi of_mi sc_mi
	
foreach wspc in twoinst_cas { 
foreach fe in FE1_as { 
foreach dvx in combat_death_by_  ndi_despair2_by_ ndi_suicide_plus2_by_ anyptsd_in_ { 
	foreach timehorizon in 8 { 

if "`dvx'" == "combat_death_by_" local lab "Combat Death" 
if "`dvx'" == "ndi_despair2_by_" local lab "Death of Despair" 
if "`dvx'" == "ndi_suicide_plus2_by_" local lab "Suicide"
if "`dvx'" == "anyptsd_in_" local lab "Any PTSD VADC" 

local x = 4*(`timehorizon')

local dv `dvx'

preserve
local yr1 2001
if inlist("`dv'","inc_combined_by_","rec_any_forc_by_") {
	drop if year_arrival == 2001
	local yr1 2002
}

local midyr 2006 

if inlist("`dv'","inc_combined_by_","noncom_death_by_","ndi_despair2_by_","ndi_suicide_plus2_by_") local midyr 2008
if inlist("`dv'","ndi_war_by_","combat_death_by_","wia_by_") local midyr 2005

local endyr = 2019 - `timehorizon'

if "`wspc'" == "twoinst_cas" noi ivreghdfe `dv'`x' $Xs an_samp_dum (${NEWENDOG} ${NEWENDOG}_casrate  = ${NEWINST} ${NEWINST}_casrate ) if ${rest} >= `x' , absorb(FE = ${`fe'}) robust $ivregsettings

keep if e(sample)


predict pred_X_and_dep 

if "`wspc'" == "twoinst_cas" {
g pred_dep = _b[${NEWENDOG}]*${NEWENDOG} + _b[${NEWENDOG}_casrate]*${NEWENDOG}_casrate
g pred_X_no_dep = pred_X_and_dep -  _b[${NEWENDOG}]*${NEWENDOG} - _b[${NEWENDOG}_casrate]*${NEWENDOG}_casrate
}
g pred_FE = FE


g C = 1
noi summ year_arrival
collapse (mean)   `dv'`x' pred_X_and_dep pred_X_no_dep pred_dep pred_FE  (sum) C, by(year_arrival)

noi tab year_arrival
noi list *

foreach v in `dv'`x' pred_X_and_dep pred_X_no_dep pred_dep pred_FE  {
summ `v' if year_arrival == `yr1' 
replace `v'= `v'-r(mean)
}

noi list * 

local ydec 3
if inlist("`dvx'","amtdisability_in_","amtvadc_in_") local ydec 0
if inlist("`dvx'","wia_by_","noncom_death_by_") local ydec 1
 
local deplab = "Dep"
if "`wspc'" == "twoinst_cas" local deplab "Dep. and Peer Cas."
if "`wspc'" == "twoinst_wia" local deplab "Dep and Peer WIA"
if "`wspc'" == "twoinst_kia" local deplab "Dep and Peer KIA"


twoway ///
 (line pred_X_and_dep year_arrival, lcolor("155 0 0") lpattern(--.) lwidth(medthick) yaxis(1)) ///
(line pred_X_no_dep year_arrival, lcolor("185 140 80") lpattern(longdash) lwidth(medthick) yaxis(1)) ///
 (line `dv'`x' year_arrival, lcolor("gs4") lpattern(line) lwidth(medthick) yaxis(1)) ///
 (line pred_dep year_arrival, lcolor("85 140 180") lpattern(-) lwidth(medthick) yaxis(1)) ///\
, yline(0, lcolor(gs3) lwidth(thin)) ytitle("`lab' (Relative to 2001)", axis(1)) ylabel(, format(%9.`ydec'fc)) ///
xtick(`yr1'(1)`endyr') xlabel(`yr1'(1)`endyr') xtitle("Cohort") legend(label(1 "Explained by Xs and `deplab'") label(2 "Explained by Xs") label(3 "Observed Outcome") label(4 "Explained by `deplab'") order(3 2 4 1) symxsize(small) size(small))

if "`dvx'" == "combat_death_by_" local outlab a 
if "`dvx'" == "ndi_despair2_by_" local outlab b
if "`dvx'" ==  "ndi_suicide_plus2_by_" local outlab c
if "`dvx'" == "anyptsd_in_" local outlab d 

graph export "${outputfolder}/FigureA10`outlab'.pdf", as(pdf) replace

restore
}
}
}
}


*************
* Figure A11
*************
*****************
*BOOTSTRAP FIGURE 

*code to get the below output files is in bootstrap_cohort_decomp_july23; could incorporate here and at least check a subset.  

/*
use "created_dta/${wholesamp}", clear
cap mkdir "created_dta/bootstrap"
global Xs init_approx_age init_white init_black init_hispanic female afqs_mi  any_moral_waiver init_afqsc init_afqsc_2 init_afqsc_3 married init_nrdep hsd_ged high_school  any_meps_med_fail_cd any_meps_alcohol_fail  any_meps_marijuana_fail any_meps_cocaine_fail any_meps_alcohol_tst_miss any_meps_marijuana_tst_miss any_meps_cocaine_tst_miss ace_first_asvabcl ace_first_asvabco ace_first_asvabel ace_first_asvabfa ace_first_asvabgm ace_first_asvabgt ace_first_asvabmm ace_first_asvabof ace_first_asvabsc cl_mi co_mi el_mi fa_mi gm_mi gt_mi mm_mi of_mi sc_mi

local Trials 1000
tempfile orig_data
save `orig_data'
set seed 3209354 
set sortseed 3209354

qui {
foreach wspc in twoinst_cas { 
foreach dvx in  noncom_death_by_  { 
 use `orig_data', clear

forvalues i = 1/`Trials' {
local timehorizon 8
if "`dvx'" == "noncom_death_by_" local lab "Non-Combat Death" 
local x = 4*(`timehorizon')

local dv `dvx'

keep  `dv'`x' $Xs an_samp_dum ${NEWENDOG} ${NEWENDOG}_casrate ${NEWINST} ${NEWINST}_casrate ${rest} $FE1_as year_arrival

qui compress
preserve
*GENERATE WEIGHT FOR BAYESIAN BOOTSTRAP
g w = (-log(runiform(0,1)))
if "`wspc'" == "twoinst_cas" noi ivreghdfe `dv'`x' $Xs (${NEWENDOG} ${NEWENDOG}_casrate  = ${NEWINST} ${NEWINST}_casrate ) if ${rest} >= `x'  [aw = w] , absorb(FE = $FE1_as) robust $ivregsettings
keep if e(sample)

predict pred_X_and_dep 
if "`wspc'" == "twoinst_cas" {
g pred_dep = _b[${NEWENDOG}]*${NEWENDOG} + _b[${NEWENDOG}_casrate]*${NEWENDOG}_casrate
g pred_X_no_dep = pred_X_and_dep -  _b[${NEWENDOG}]*${NEWENDOG} - _b[${NEWENDOG}_casrate]*${NEWENDOG}_casrate
}

g beta_base = _b[${NEWENDOG}]
g beta_int = _b[${NEWENDOG}_casrate]

g pred_FE = FE
g `dv'`x'_w = `dv'`x' * w 

collapse (mean)   `dv'`x' `dv'`x'_w w pred_X_and_dep pred_X_no_dep pred_dep pred_FE beta_base beta_int, by(year_arrival) // (sum) C
save "created_dta/bootstrap/bootstrap_trial_`i'.dta", replace

cap log close 

restore
}
}
}
}

local trials 1000 //`Trials'
local x 32
local wspc twoinst_cas
local dv  noncom_death_by_ 

use created_dta/bootstrap/bootstrap_trial_1.dta, clear
gen trial = 1
	foreach var in `dv'`x' pred_X_and_dep pred_X_no_dep pred_dep {
		qui summ `var' if year_arrival == 2001
		qui replace `var' = `var' - r(mean)
	}
forvalues i = 2/`trials' {
	qui append using created_dta/bootstrap/bootstrap_trial_`i'.dta
	qui replace trial = `i' if trial == .
	foreach var in `dv'`x' pred_X_and_dep pred_X_no_dep pred_dep {
		qui summ `var' if trial == `i' & year_arrival == 2001
		qui replace `var' = `var' - r(mean) if trial == `i'
	}
}

save created_dta/bootstrappedoutput.dta, replace 
*/

use created_dta/bootstrappedoutput.dta, clear 

local x 32 
local dv noncom_death_by_ 
bys trial: egen varY = var(`dv'`x')  if year_arrival != 2001
g covX = . 
g R2X = . 
g covdep = .
g R2dep = .
g covXdep = .
g R2Xdep = .

qui {
forvalues trial = 1(1)1000 {
correlate `dv'`x' pred_X_no_dep  if year_arrival != 2001 & trial == `trial', covariance 
replace covX = r(cov_12) if year_arrival != 2001 & trial == `trial'
replace R2X = covX/varY if year_arrival != 2001 & trial == `trial'

correlate `dv'`x' pred_dep if year_arrival != 2001 & trial == `trial', covariance
replace covdep = r(cov_12) if year_arrival != 2001 & trial == `trial'
replace R2dep = covdep/varY if year_arrival != 2001 & trial == `trial'

correlate `dv'`x' pred_X_and_dep if year_arrival != 2001 & trial == `trial' , covariance
replace covXdep = r(cov_12) if year_arrival != 2001 & trial == `trial'
replace R2Xdep = covXdep/varY if year_arrival != 2001 & trial == `trial'
}
}

preserve 
keep trial R2dep 
duplicates drop 
sort R2dep
sum R2dep, det
sum  trial if _n == 951
local whicht = r(mean)
sum  trial if _n == 501
local whicht2 = r(mean)
restore 

summ R2dep, det 
summ R2dep if trial == `whicht', det 
summ R2dep if trial == `whicht2', det 

g PLOTTHIS = 1 if trial == `whicht'
g PLOTTHIS2 = 1 if trial == `whicht2'

local x 32 
local dv noncom_death_by_ 
sort trial year_arrival
twoway ///
(line pred_dep year_arrival if PLOTTHIS==1, lcolor("blue") lpattern(.-) lwidth(medthick) yaxis(1)) ///
(line pred_dep year_arrival if PLOTTHIS2==1, lcolor("85 140 180") lpattern(-) lwidth(medthick) yaxis(1)) ///
 (line `dv'`x' year_arrival if PLOTTHIS==1, lcolor("gs4") lpattern(line) lwidth(medthick) yaxis(1)) ///
, ytitle("Non-Combat Death (Relative to 2001)", axis(1)) ylabel(, format(%9.1fc)) yline(0, lcolor(gs4) lwidth(thin)) ///
xtick(2001(1)2011) xlabel(2001(1)2011) xtitle("Cohort") legend(label(1 "Explained by Dep. & Peer Cas. (95th %ile)") label(2 "Explained by Dep. & Peer Cas. (50th %ile)") label(3 "Observed Outcome") order(3 1 2) symxsize(small) size(small))
graph export "${outputfolder}/FigureA11.pdf", as(pdf) replace


*************
* Figure A12 
*************

use "created_dta/${analysissamp}" , clear
keep if year_arrival >= 2005 
cap g  mos_tc_post_yr_terms_as = $FE1 
forv i = 1/36{
	gen amtssissdi_in_`i' = amtdisability_in_`i' - amtvadc_in_`i'
}

	global Xs init_approx_age init_white init_black init_hispanic female afqs_mi  any_moral_waiver init_afqsc init_afqsc_2 init_afqsc_3 married init_nrdep hsd_ged high_school  any_meps_med_fail_cd any_meps_alcohol_fail  any_meps_marijuana_fail any_meps_cocaine_fail any_meps_alcohol_tst_miss any_meps_marijuana_tst_miss any_meps_cocaine_tst_miss ace_first_asvabcl ace_first_asvabco ace_first_asvabel ace_first_asvabfa ace_first_asvabgm ace_first_asvabgt ace_first_asvabmm ace_first_asvabof ace_first_asvabsc cl_mi co_mi el_mi fa_mi gm_mi gt_mi mm_mi of_mi sc_mi

foreach wspc in twoinst_cas_orig { 
	foreach fe in FE1_as { 
		foreach dvx in wia_by_ noncom_death_by_ amtvadc_in_ amtssissdi_in_ { 
			foreach timehorizon in 8 { 
				
if "`dvx'" == "wia_by_" local lab "Combat Injury" 
if "`dvx'" == "noncom_death_by_" local lab "Non-Combat Death" 
if "`dvx'" == "amtvadc_in_" local lab "Amt VADC"
if "`dvx'" == "amtssissdi_in_" local lab "Amt SSI/SSDI" 
			
local x = 4*(`timehorizon')

local dv `dvx'

preserve
local yr1 2005
if inlist("`dv'","inc_combined_by_","rec_any_forc_by_") {
	drop if year_arrival == 2005
	local yr1 2006
}

local midyr 2008

local endyr = 2019 - `timehorizon'

if "`wspc'" == "twoinst_cas_orig" noi ivreghdfe `dv'`x' $Xs (${NEWENDOG} ${NEWENDOG}_casrate = ${NEWINST} ${NEWINST}_casrate) if ${rest} >= `x' , absorb(FE = ${`fe'}) robust

keep if e(sample)

predict pred_X_and_dep 

if "`wspc'" == "twoinst_cas_orig" {
g pred_dep = _b[${NEWENDOG}]*${NEWENDOG} + _b[${NEWENDOG}_casrate]*${NEWENDOG}_casrate 
g pred_X_no_dep = pred_X_and_dep - (_b[${NEWENDOG}]*${NEWENDOG} + _b[${NEWENDOG}_casrate]*${NEWENDOG}_casrate) 
}

g pred_FE = FE

g C = 1
noi summ year_arrival
collapse (mean)   `dv'`x' pred_X_and_dep pred_X_no_dep pred_dep pred_FE  (sum) C, by(year_arrival)

noi tab year_arrival
noi list *

foreach v in `dv'`x' pred_X_and_dep pred_X_no_dep pred_dep pred_FE  {
summ `v' if year_arrival == `yr1' 
replace `v'= `v'-r(mean)
}


noi list * 

local ydec 3
if inlist("`dvx'","amtdisability_in_","amtvadc_in_","amtssissdi_in_") local ydec 0
if inlist("`dvx'","wia_by_","noncom_death_by_") local ydec 1
 
local deplab = "Dep"
if "`wspc'" == "twoinst_cas" local deplab "Dep. and Peer Cas."
if "`wspc'" == "twoinst_wia" local deplab "Dep and Peer WIA"
if "`wspc'" == "twoinst_kia" local deplab "Dep and Peer KIA"

keep if inrange(year_arrival, `yr1', `endyr')

twoway ///
 (line pred_X_and_dep year_arrival, lcolor("155 0 0") lpattern(--.) lwidth(medthick) yaxis(1)) ///
(line pred_X_no_dep year_arrival, lcolor("185 140 80") lpattern(longdash) lwidth(medthick) yaxis(1)) ///
 (line `dv'`x' year_arrival, lcolor("gs4") lpattern(line) lwidth(medthick) yaxis(1)) ///
 (line pred_dep year_arrival, lcolor("85 140 180") lpattern(-) lwidth(medthick) yaxis(1)) ///\
, yline(0, lcolor(gs3) lwidth(thin)) ytitle("`lab' (Relative to 2005)", axis(1)) ylabel(, format(%9.`ydec'fc)) ///
xtick(`yr1'(1)`endyr') xlabel(`yr1'(1)`endyr') xtitle("Cohort") legend(label(1 "Explained by Xs and `deplab'") label(2 "Explained by Xs") label(3 "Observed Outcome") label(4 "Explained by `deplab'") order(3 2 4 1) symxsize(small) size(small))

if "`dvx'" == "wia_by_" local outlab a 
if "`dvx'" == "noncom_death_by_" local outlab b
if "`dvx'" ==  "amtvadc_in_" local outlab c
if "`dvx'" == "amtssissdi_in_" local outlab d 

graph export "${outputfolder}/FigureA12`outlab'.pdf", as(pdf) replace


restore
}
}
}
}




use "created_dta/${wholesamp}", clear
forv i = 1/36{
	gen amtssissdi_in_`i' = amtdisability_in_`i' - amtvadc_in_`i'
}

*************
* Figure A13
*************
	
	global Xs init_approx_age init_white init_black init_hispanic female afqs_mi  any_moral_waiver init_afqsc init_afqsc_2 init_afqsc_3 married init_nrdep hsd_ged high_school  any_meps_med_fail_cd any_meps_alcohol_fail  any_meps_marijuana_fail any_meps_cocaine_fail any_meps_alcohol_tst_miss any_meps_marijuana_tst_miss any_meps_cocaine_tst_miss ace_first_asvabcl ace_first_asvabco ace_first_asvabel ace_first_asvabfa ace_first_asvabgm ace_first_asvabgt ace_first_asvabmm ace_first_asvabof ace_first_asvabsc cl_mi co_mi el_mi fa_mi gm_mi gt_mi mm_mi of_mi sc_mi
	
	qui {
	foreach wspc in IV  { 
	foreach fe in FE1_as { 
	foreach dvx in amtvadc_in_ { 
	forvalues timehorizon = 1(1)14 { 
	if "`dvx'" == "amtvadc_in_" local lab "Amt VADC" 

	local x = 4*(`timehorizon')

	local dv `dvx'
	
	preserve
	local yr1 2001
	if inlist("`dv'","inc_combined_by_","rec_any_forc_by_") {
		drop if year_arrival == 2001
		local yr1 2002
	}
	local midyr 2007
	if inlist("`dv'","inc_combined_by_","noncom_death_by_","ndi_despair2_by_","ndi_suicide_plus2_by_") local midyr 2008
	if inlist("`dv'","ndi_war_by_","combat_death_by_") local midyr 2005

	if "`wspc'" == "IV" noi ivreghdfe `dv'`x' $Xs an_samp_dum (${NEWENDOG}  = ${NEWINST} ) if ${rest} >= `x' , absorb(FE = ${`fe'}) robust $ivregsettings
	if "`wspc'" == "twoinst_cas" noi ivreghdfe `dv'`x' $Xs an_samp_dum (${NEWENDOG} ${NEWENDOG}_casrate  = ${NEWINST} ${NEWINST}_casrate ) if ${rest} >= `x' , absorb(FE = ${`fe'}) robust $ivregsettings

	keep if e(sample)

	predict pred_X_and_dep 
	if "`wspc'" == "IV" {
	g pred_dep = _b[${NEWENDOG}]*${NEWENDOG}
	g pred_X_no_dep = pred_X_and_dep -  _b[${NEWENDOG}]*${NEWENDOG}
	}

	if "`wspc'" == "twoinst_cas" {
	g pred_dep = _b[${NEWENDOG}]*${NEWENDOG} + _b[${NEWENDOG}_casrate]*${NEWENDOG}_casrate
	g pred_X_no_dep = pred_X_and_dep -  _b[${NEWENDOG}]*${NEWENDOG} - _b[${NEWENDOG}_casrate]*${NEWENDOG}_casrate
	}
	g pred_FE = FE

	g C = 1
	noi summ year_arrival
	collapse (mean)   `dv'`x' pred_X_and_dep pred_X_no_dep pred_dep pred_FE  (sum) C, by(year_arrival)

	noi tab year_arrival
	noi list *
	
	g yrs_since_app = `timehorizon'

	save "created_dta/decompresults_`timehorizon'yrout_nrdep.dta" , replace 
	restore
	}
	}
	}
	}
	}


	use "created_dta/decompresults_1yrout_nrdep.dta", clear 
	forvalues x = 2(1)14 { 
		append using "created_dta/decompresults_`x'yrout_nrdep.dta"
	}

		g amtvadc = .
		forvalues x = 1(1)14 { 
		local y = `x'*4
		replace amtvadc = amtvadc_in_`y' if amtvadc== . 
		}
		order year_arrival yrs_since_app amtvadc
		keep year_arrival yrs_since_app amtvadc pred* 

	g amtvadc_unadj = amtvadc

	foreach v in amtvadc pred_X_and_dep pred_X_no_dep pred_dep pred_FE  {
	forvalues x = 1(1)14 {
	summ `v' if year_arrival == 2001 & yrs_since_app == `x'
	replace `v'= `v'-r(mean) if yrs_since_app == `x'
	}
	}

	bys yrs_since_app (year_arrival): egen base_2001  = max(amtvadc_unadj*(year_arrival==2001))

	rename year_arrival cohort 
	g cal_yr = cohort + yrs_since

	g amt_vadc_left_afterX = amtvadc - pred_X_no_dep
	g amt_vadc_left_afterDep = amtvadc - pred_dep
	g amt_vadc_left_afterXandDep = amtvadc - pred_X_and_dep

	g amt_vadc_diff = amtvadc
	g amt_vadc_left_afterXandDep_diff = amt_vadc_left_afterXandDep

	replace amtvadc= amtvadc + base_2001
	replace amt_vadc_left_afterX = amt_vadc_left_afterX + base_2001 
	replace amt_vadc_left_afterDep = amt_vadc_left_afterDep + base_2001 
	replace amt_vadc_left_afterXandDep = amt_vadc_left_afterXandDep + base_2001 

	foreach out in amtvadc  amt_vadc_left_afterXandDep { 
	twoway  ///
	(line `out' cal_yr if cohort == 2001, lcolor(black)) ///
	(line `out' cal_yr if cohort == 2002, lcolor(purple)) ///
	(line `out' cal_yr if cohort == 2003, lcolor(purple%66)) ///
	(line `out' cal_yr if cohort == 2004, lcolor(cranberry)) ///
	(line `out' cal_yr if cohort == 2005, lcolor(cranberry%66)) ///
	(line `out' cal_yr if cohort == 2006, lcolor(pink%33)) ///
	(line `out' cal_yr if cohort == 2007, lcolor(sienna%66)) ///
	(line `out' cal_yr if cohort == 2008, lcolor(dkorange%66)) ///
	(line `out' cal_yr if cohort == 2009, lcolor(orange%66)) ///
	(line `out' cal_yr if cohort == 2010, lcolor(gold)) ///
	(line `out' cal_yr if cohort == 2011, lcolor(gold%66)) ///
	(line `out' cal_yr if cohort == 2012, lcolor(gold%33)) ///
	(line `out' cal_yr if cohort == 2013, lcolor(sand%66)) ///
	(line `out' cal_yr if cohort == 2014, lcolor(sand%33)) ///
	(line `out' cal_yr if cohort == 2015, lcolor(yellow)) ///
	(scatter `out' cal_yr if yrs_since_app == 8, msymbol(x) mcolor(black)), ///
	legend(label(1 "2001") label(2 "2002") label(3 "2003") ///
	label(4 "2004") label(5 "2005") label(6 "2006") ///
	label(7 "2007") label(8 "2008") label(9 "2009") ///
	label(10 "2010") label(11 "2012") label(12 "2013") ///
	label(13 "2014")  label(14 "2015") label(15 "2015") label(16 "8 Yrs") symysize(0.05cm) symx(0.05cm)  cols(8) size(small) textw(vsmall) bm(zero) region(lstyle(none)) ) ///
	ytitle(VADC Amount) xtitle(Calendar Year) ///
	yscale(range(0 10000)) ylabel(0(2000)10000) ytick(0(2000)10000) ///
	xlabel(2003(2)2019) xtick(2003(1)2019)
	
	if "`out'" == "amtvadc" local outlab a
	if "`out'" == "amt_vadc_left_afterXandDep"  local outlab b

	graph export "${outputfolder}/FigureA13`outlab'.pdf", as(pdf) replace
	}
	

	foreach out in amt_vadc_diff amt_vadc_left_afterXandDep_diff {
	twoway  ///
	(line `out' cal_yr if cohort == 2002, lcolor(purple)) ///
	(line `out' cal_yr if cohort == 2003, lcolor(purple%66)) ///
	(line `out' cal_yr if cohort == 2004, lcolor(cranberry)) ///
	(line `out' cal_yr if cohort == 2005, lcolor(cranberry%66)) ///
	(line `out' cal_yr if cohort == 2006, lcolor(pink%33)) ///
	(line `out' cal_yr if cohort == 2007, lcolor(sienna%66)) ///
	(line `out' cal_yr if cohort == 2008, lcolor(dkorange%66)) ///
	(line `out' cal_yr if cohort == 2009, lcolor(orange%66)) ///
	(line `out' cal_yr if cohort == 2010, lcolor(gold)) ///
	(line `out' cal_yr if cohort == 2011, lcolor(gold%66)) ///
	(line `out' cal_yr if cohort == 2012, lcolor(gold%33)) ///
	(line `out' cal_yr if cohort == 2013, lcolor(sand%66)) ///
	(line `out' cal_yr if cohort == 2014, lcolor(sand%33)) ///
	(line `out' cal_yr if cohort == 2015, lcolor(yellow)) ///
	(scatter `out' cal_yr if yrs_since_app == 8, msymbol(x) mcolor(black)), ///
	legend( label(1 "2002") label(2 "2003") ///
	label(3 "2004") label(4 "2005") label(5 "2006") ///
	label(6 "2007") label(7 "2008") label(8 "2009") ///
	label(9 "2010") label(10 "2012") label(11 "2013") ///
	label(12 "2014")  label(13 "2015") label(14 "2015") label(15 "8 Yrs") cols(8) size(small) symysize(0.05cm) symx(0.05cm) textw(vsmall)  bm(zero) region(lstyle(none)) ) ytitle(VADC Amount) xtitle(Calendar Year) ///
	yscale(range(0 10000)) ylabel(0(2000)10000) ytick(0(2000)10000) ///
	xlabel(2003(2)2019) xtick(2003(1)2019)

	if "`out'" == "amt_vadc_diff" local outlab c
	if "`out'" == "amt_vadc_left_afterXandDep_diff"  local outlab d

	graph export "${outputfolder}/FigureA13`outlab'.pdf", as(pdf) replace

	}
	


*************
* Figure A14
*************

use "created_dta/${analysissamp}", clear

g ssissdi_amt_in_32 = amtdisability_in_32 - amtvadc_in_32
g ssissdi_amt_in_24 =  amtdisability_in_24 - amtvadc_in_24

egen mths_dep_win_6yr = rowtotal(deployed_0-deployed_71)
egen mths_dep_win_8yr = rowtotal(deployed_0-deployed_95)

g dep_win_6yr = mths_dep_win_6yr > 0 
replace  dep_win_6yr = . if mths_dep_win_6yr== . 

g dep_win_8yr = mths_dep_win_8yr > 0
replace  dep_win_8yr = . if mths_dep_win_8yr== . 

global OUTCOMESET  combat_death_by_ amtvadc_in_ ssissdi_amt_in_ noncom_death_by_


foreach outcome in $OUTCOMESET   {
 
 
preserve 
clear 
set obs 20
g beta = . 
g se = . 
g N = . 
g y0_beta =. 
g sey0 = . 
g Ny0 = .
g rawmean = . 
g rawmean_6yr = . 
g rawmean_8yr = . 
g cohortstart = 2004 + _n  
g outcome = ""
save "created_dta/rolling_cohorts_8yr_`outcome'", replace 
restore 


if "`outcome'" != "vantage_v4_score731" {	
g Y0`outcome'32 = -`outcome'32*(1-dep_win_3yr)
g Y0`outcome'24 = -`outcome'24*(1-dep_win_3yr)
}

if "`outcome'" == "vantage_v4_score731" {	
g Y0`outcome' = - `outcome'*(1-dep_win_3yr)
g Y02`outcome' = - `outcome'*(1-dep_win_3yr)
}


	foreach cohortset in 2005 2006 2007 2008 2009 {
		local cohortsetplus2 = `cohortset' + 2
if "`outcome'" != "vantage_v4_score731" {	
ivreghdfe `outcome'32 (${NEWENDOG} = ${NEWINST}) if ${rest} >= 32 & inrange(year_arrival,`cohortset',`cohortsetplus2'), absorb(${FE1}) robust
	summ `outcome'32 if dep_win_3yr == 0 & e(sample)
	local rawmean = r(mean)
	summ `outcome'32 if dep_win_6yr == 0 & e(sample)
	local rawmean6 = r(mean)
	summ `outcome'32 if dep_win_8yr == 0 & e(sample)
	local rawmean8 = r(mean)
}
if "`outcome'" == "vantage_v4_score731" {	
ivreghdfe `outcome' (${NEWENDOG} = ${NEWINST}) if ${rest} >= 32 & inrange(year_arrival,`cohortset',`cohortsetplus2'), absorb(${FE1}) robust
summ `outcome' if dep_win_3yr == 0 & e(sample)
	local rawmean = r(mean)
	summ `outcome' if dep_win_6yr == 0 & e(sample)
	local rawmean6 = r(mean)
	summ `outcome' if dep_win_8yr == 0 & e(sample)
	local rawmean8 = r(mean)
}

	
	preserve 
	use "created_dta/rolling_cohorts_8yr_`outcome'", clear 
	replace beta = _b[${NEWENDOG}]*10 if cohortstart == `cohortset'
	replace se = _se[${NEWENDOG}]*10 if cohortstart == `cohortset'
	replace N = e(N) if cohortstart == `cohortset'
	replace rawmean = `rawmean' if cohortstart == `cohortset'
	replace rawmean_6yr = `rawmean6' if cohortstart == `cohortset'
	replace rawmean_8yr = `rawmean8' if cohortstart == `cohortset'
	
	save "created_dta/rolling_cohorts_8yr_`outcome'", replace 
	restore
	
if "`outcome'" != "vantage_v4_score731" {	
ivreghdfe Y0`outcome'32 (dep_win_3yr = ${NEWINST}) if ${rest} >= 32 & inrange(year_arrival,`cohortset',`cohortsetplus2'), absorb(${FE1}) robust
}
if "`outcome'" == "vantage_v4_score731" {	
ivreghdfe Y0`outcome' (dep_win_3yr = ${NEWINST}) if ${rest} >= 32 & inrange(year_arrival,`cohortset',`cohortsetplus2'), absorb(${FE1}) robust
}
	preserve 
	use "created_dta/rolling_cohorts_8yr_`outcome'", clear 
	replace y0_beta = _b[dep_win_3yr] if cohortstart == `cohortset'
	replace sey0 = _se[dep_win_3yr] if cohortstart == `cohortset'
	replace Ny0 = e(N) if cohortstart == `cohortset'
	save "created_dta/rolling_cohorts_8yr_`outcome'", replace 
	restore

	}
}	

 global OUTCOMESET2  combat_death_by_ ssissdi_amt_in_ noncom_death_by_ 

use "created_dta/rolling_cohorts_8yr_amtvadc_in_", clear 
replace outcome = "amtvadc_in_"
foreach outcome in $OUTCOMESET2 {
append using "created_dta/rolling_cohorts_8yr_`outcome'"
replace outcome = "`outcome'" if outcome == ""
}
save "created_dta/rolling_cohorts_8yr_all", replace 

use "created_dta/rolling_cohorts_8yr_all", clear 
g ub = beta + 1.96*se
g lb = beta - 1.96*se

g ub2 = y0_beta + 1.96*sey0
g lb2 = y0_beta - 1.96*sey0

drop if beta == . 

g y10 = y0_beta + beta 
g ub10 = y10 + 1.96*se 
g lb10 = y10 - 1.96*se 

replace y0_beta = 0 if outcome == "combat_death_by_"
replace y10 = beta if outcome == "combat_death_by_"
replace ub10 = y10 + 1.96*se if outcome == "combat_death_by_" 
replace lb10 = y10 - 1.96*se if outcome == "combat_death_by_" 

foreach outcome in combat_death_by_  amtvadc_in_   ssissdi_amt_in_ noncom_death_by_ { 
if "`outcome'" == "combat_death_by_" local ylab "Combat Death"
if "`outcome'" == "amtvadc_in_" local ylab "VADC Amount"
if "`outcome'" == "ssissdi_amt_in_" local ylab "SSI or SSDI Amount"
if "`outcome'" == "noncom_death_by_" local ylab "Noncombat Death"

twoway (scatter y10 cohortstart if outcome == "`outcome'", mcolor(black) yaxis(1)) (line y10 cohortstart if outcome == "`outcome'", mcolor(black) yaxis(1))  (rcap ub10 lb10 cohortstart if outcome == "`outcome'", lcolor(gs7) yaxis(1))  (scatter y0_beta cohortstart if outcome == "`outcome'", mcolor(gs5) yaxis(1)) (line y0_beta cohortstart if outcome == "`outcome'", mcolor(gs5) yaxis(1)),  legend(order(1 4) label(1 "Deploying Compliers (10 Months Dep.)") label(4 "Nondeploying Compliers")) xtitle("Cohorts") ytitle("`ylab'") ///
xlabel(2005 "05-07" 2006 "06-08" 2007 "07-09" 2008 "08-10" 2009 "09-11"  ) xscale(range(2005 2009)) xtick(2005(1)2009) 


if "`outcome'" == "combat_death_by_" local outcomefilename a
if "`outcome'" == "amtvadc_in_" local outcomefilename b
if "`outcome'" == "ssissdi_amt_in_" local outcomefilename c
if "`outcome'" == "noncom_death_by_" local outcomefilename d

graph export "${outputfolder}/FigureA14`outcomefilename'.pdf", replace as(pdf) 

}


*************
* Figure A15
*************
	use "created_dta/${analysissamp}", clear

g ssissdi_amt_in_32 = amtdisability_in_32 - amtvadc_in_32
g ssissdi_amt_in_24 =  amtdisability_in_24 - amtvadc_in_24

egen mths_dep_win_6yr = rowtotal(deployed_0-deployed_71)
egen mths_dep_win_8yr = rowtotal(deployed_0-deployed_95)


g dep_win_6yr = mths_dep_win_6yr > 0 
replace  dep_win_6yr = . if mths_dep_win_6yr== . 

g dep_win_8yr = mths_dep_win_8yr > 0
replace  dep_win_8yr = . if mths_dep_win_8yr== . 

global OUTCOMESET  inc_combined_by_ nsc_enr_postarr_by_ vantage_v4_score731

foreach outcome in $OUTCOMESET   {
 
if "`outcome'" != "vantage_v4_score731" {	
g Y0`outcome'32 = -`outcome'32*(1-dep_win_3yr)
g Y0`outcome'24 = -`outcome'24*(1-dep_win_3yr)
}

if "`outcome'" == "vantage_v4_score731" {	
g Y0`outcome' = - `outcome'*(1-dep_win_3yr)
g Y02`outcome' = - `outcome'*(1-dep_win_3yr)
}

preserve 
clear 
set obs 20
g beta = . 
g se = . 
g N = . 
g y0_beta =. 
g sey0 = .
g Ny0 = . 
g rawmean = . 
g rawmean_6yr = . 
g rawmean_8yr = . 
g cohortstart = 2004 + _n
g outcome = ""  
save "created_dta/rolling_cohorts_6yr_`outcome'", replace 
restore 

	foreach cohortset in 2005 2006 2007 2008 2009 2010 2011 {
		local cohortsetplus2 = `cohortset' + 2
if "`outcome'" != "vantage_v4_score731" {			
ivreghdfe `outcome'24 (${NEWENDOG} = ${NEWINST}) if ${rest} >= 24 & inrange(year_arrival,`cohortset',`cohortsetplus2'), absorb(${FE1}) robust $ivregsettings
	summ `outcome'24 if dep_win_3yr == 0 & e(sample)
	local rawmean = r(mean)
	summ `outcome'24 if dep_win_6yr == 0 & e(sample)
	local rawmean6 = r(mean)
	summ `outcome'24 if dep_win_8yr == 0 & e(sample)
	local rawmean8 = r(mean)
}
if "`outcome'" == "vantage_v4_score731" {			
ivreghdfe `outcome' (${NEWENDOG} = ${NEWINST}) if ${rest} >= 24 & inrange(year_arrival,`cohortset',`cohortsetplus2'), absorb(${FE1}) robust $ivregsettings
	summ `outcome' if dep_win_3yr == 0 & e(sample)
	local rawmean = r(mean)
	summ `outcome' if dep_win_6yr == 0 & e(sample)
	local rawmean6 = r(mean)
	summ `outcome' if dep_win_8yr == 0 & e(sample)
	local rawmean8 = r(mean)
}


	preserve 
	use "created_dta/rolling_cohorts_6yr_`outcome'", clear 
	replace beta = _b[${NEWENDOG}]*10 if cohortstart == `cohortset'
	replace se = _se[${NEWENDOG}]*10 if cohortstart == `cohortset'
	replace N = e(N) if cohortstart == `cohortset'
	replace rawmean = `rawmean' if cohortstart == `cohortset'
	replace rawmean_6yr = `rawmean6' if cohortstart == `cohortset'
	replace rawmean_8yr = `rawmean8' if cohortstart == `cohortset'	
	save "created_dta/rolling_cohorts_6yr_`outcome'", replace 
	restore


if "`outcome'" != "vantage_v4_score731" {	
ivreghdfe Y0`outcome'24 (dep_win_3yr = ${NEWINST}) if ${rest} >= 24 & inrange(year_arrival,`cohortset',`cohortsetplus2'), absorb(${FE1}) robust $ivregsettings
}
if "`outcome'" == "vantage_v4_score731" {	
ivreghdfe Y02`outcome' (dep_win_3yr = ${NEWINST}) if ${rest} >= 24 & inrange(year_arrival,`cohortset',`cohortsetplus2'), absorb(${FE1}) robust $ivregsettings
}
	preserve 
	use "created_dta/rolling_cohorts_6yr_`outcome'", clear 
	replace y0_beta = _b[dep_win_3yr] if cohortstart == `cohortset'
	replace sey0 = _se[dep_win_3yr] if cohortstart == `cohortset'
	replace Ny0 = e(N) if cohortstart == `cohortset'
	save "created_dta/rolling_cohorts_6yr_`outcome'", replace 
	restore
	}
}

 global OUTCOMESET2 nsc_enr_postarr_by_ vantage_v4_score731

use "created_dta/rolling_cohorts_6yr_inc_combined_by_", clear 
replace outcome = "inc_combined_by_"
foreach outcome in $OUTCOMESET2 {
append using "created_dta/rolling_cohorts_6yr_`outcome'"
replace outcome = "`outcome'" if outcome == ""
}
save "created_dta/rolling_cohorts_6yr_all2", replace 

use "created_dta/rolling_cohorts_6yr_all2", clear 
g ub = beta + 1.96*se
g lb = beta - 1.96*se

g ub2 = y0_beta + 1.96*sey0
g lb2 = y0_beta - 1.96*sey0

drop if beta == . 

g y10 = y0_beta + beta 
g ub10 = y10 + 1.96*se 
g lb10 = y10 - 1.96*se 

replace y0_beta = 0 if outcome == "combat_death_by_"
replace y10 = beta if outcome == "combat_death_by_"
replace ub10 = y10 + 1.96*se if outcome == "combat_death_by_" 
replace lb10 = y10 - 1.96*se if outcome == "combat_death_by_" 

foreach outcome in inc_combined_by_ nsc_enr_postarr_by_ vantage_v4_score731  {
if "`outcome'" == "inc_combined_by_" local ylab "Incarceration"
if "`outcome'" == "nsc_enr_postarr_by_" local ylab "Any Postsecondary Enrollment"
if "`outcome'" == "vantage_v4_score731" local ylab "Vantage Credit Score"

twoway (scatter y10 cohortstart if outcome == "`outcome'", mcolor(black) yaxis(1)) (line y10 cohortstart if outcome == "`outcome'", mcolor(black) yaxis(1))  (rcap ub10 lb10 cohortstart if outcome == "`outcome'", lcolor(gs7) yaxis(1))  (scatter y0_beta cohortstart if outcome == "`outcome'", mcolor(gs5) yaxis(1)) (line y0_beta cohortstart if outcome == "`outcome'", mcolor(gs5) yaxis(1)),  legend(order(1 4) label(1 "Deploying Compliers (10 Months Dep.)") label(4 "Nondeploying Compliers")) xtitle("Cohorts") ytitle("`ylab'") ///
xlabel(2005 "05-07" 2006 "06-08" 2007 "07-09" 2008 "08-10" 2009 "09-11" 2010 "10-12" 2011 "11-13"    ) xscale(range(2005 2011)) xtick(2005(1)2011) 

if "`outcome'" == "inc_combined_by_" local outcomefilename a
if "`outcome'" == "nsc_enr_postarr_by_" local outcomefilename b
if "`outcome'" == "vantage_v4_score731" local outcomefilename c

graph export "${outputfolder}/FigureA15`outcomefilename'.pdf", replace as(pdf) 

}

